/*
 *
 * http://www.paulbourke.net/dataformats/tga/
 */

#pragma once

#include "core/color.h"
#include "public/build.h"
#include "image.h"

#include <string>

namespace renderit
{
	namespace image
	{
		class loader_tga
		{
		public:
			loader_tga();
			~loader_tga();

		public:
			image * load(const std::string& filename);

			bool save_as(const std::string& filename, bool only_raw_data = false);

			bool save(bool only_raw_data = false);
			
		public:
			core::color4b * get_colors() const
			{ 
				return color_; 
			}

			core::color4b * move_colors()
			{
				auto c = color_;
				color_ = nullptr;
				return c;
			}

			int width() const { return tga_header_.width; }
			int height() const { return tga_header_.height; }

		public:
			core::color4b& get_at(uint32 row, uint32 line)
			{
				row = kclamp(row, 0, tga_header_.height - 1);
				line = kclamp(line, 0, tga_header_.width - 1);
				return color_[row * tga_header_.width + line];
			}

		private:			
			core::color4b * color_;

			std::string fpath_;

#pragma pack(push, 1)
			struct tga_header
			{
				uint8 identsize;
				uint8 colourmaptype;
				uint8 imagetype;

				uint16 colourmapstart;
				uint16 colourmaplength;
				uint8 colourmapbits;

				uint16 xstart;
				uint16 ystart;
				uint16 width;
				uint16 height;
				uint8 bits;
				uint8 descriptor;
			} tga_header_;
#pragma pack(pop)
		};
	}
}